package com.wodsy.phoenix.apps.search.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.goodsc.model.search.SearchResultList;
import com.wodsy.phoenix.search.GoodsSearch;

@Controller
@RequestMapping("/")
public class SearchController {

	@Reference(interfaceClass = GoodsSearch.class, interfaceName = "com.wodsy.phoenix.search.GoodsSearch")
	private GoodsSearch goodsSearch;

	@RequestMapping(value = "psearch" ,method = RequestMethod.GET)
	public String psearch(@RequestParam(required = true) String keyword,
			@RequestParam(required = true) String sort,
			@RequestParam(required = true) boolean price,
			@RequestParam(required = true) int min,
			@RequestParam(required = true) int max,
			@RequestParam(required = true) int pno,
			@RequestParam(required = true) int psize,
			Map<String, Object> model) {
		
		PhoenixResponseWrapper<SearchResultList> wrapper = null;
		if(sort.equals("default")){
			wrapper = goodsSearch.searchGoodsBykeyVSDefault(keyword, pno, psize, min, max, price);
		}else if(sort.equals("popularity")){
			wrapper = goodsSearch.searchGoodsBykeyVSPopularity(keyword, pno, psize, min, max, price);
		}else if(sort.equals("sales")){
			wrapper = goodsSearch.searchGoodsBykeyVSSales(keyword, pno, psize, min, max, price);
		}else if(sort.equals("price")){
			wrapper = goodsSearch.searchGoodsBykeyVSPrice(keyword, pno, psize, min, max, price);
		}
		
		if(wrapper != null){
			model.put("pager", wrapper.getObj());
		}
		
		return "search";
	}
	
	@RequestMapping(value = "asearch" ,method = RequestMethod.GET)
	@ResponseBody
	public SearchResultList asearch(@RequestParam(required = true) String keyword,
			@RequestParam(required = true) String sort,
			@RequestParam(required = true) boolean price,
			@RequestParam(required = true) int min,
			@RequestParam(required = true) int max,
			@RequestParam(required = true) int pno,
			@RequestParam(required = true) int psize) {
		
		PhoenixResponseWrapper<SearchResultList> wrapper = null;
		
		if(sort.equals("default")){
			wrapper = goodsSearch.searchGoodsBykeyVSDefault(keyword, pno, psize, min, max, price);
		}else if(sort.equals("popularity")){
			wrapper = goodsSearch.searchGoodsBykeyVSPopularity(keyword, pno, psize, min, max, price);
		}else if(sort.equals("sales")){
			wrapper = goodsSearch.searchGoodsBykeyVSSales(keyword, pno, psize, min, max, price);
		}else if(sort.equals("price")){
			wrapper = goodsSearch.searchGoodsBykeyVSPrice(keyword, pno, psize, min, max, price);
		}
		if(wrapper == null){
			return null;
		}else{
			return wrapper.getObj();
		}
	}
}
